package com.capgemini.remind.controller;

import com.capgemini.commons.DesUtil;
import com.capgemini.core.AjaxResponse;
import com.capgemini.interceptor.DealerNumUtils;
import com.capgemini.interceptor.SecurityCONST;
import com.capgemini.remind.service.CommonsService;
import com.capgemini.remind.service.RemindService;
import com.capgemini.remind.service.RemindSmsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {
	public final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private CommonsService commonsService;


	@RequestMapping("/auto")
	public String autoLogin(HttpSession session, String apwd) {
		try {
			Long now = System.currentTimeMillis();

			String cipherText = DesUtil.decrypt(apwd, "capgemini!@#");

			String[] data = cipherText.split("@");

			Long time = Long.valueOf(data[1]);

			if(now - time < 20000){
				session.setAttribute(SecurityCONST.IS_LOGIN, true);
				session.setAttribute(SecurityCONST.DEALER_NUM, data[0]);

				return "redirect:/remind/report/manage/" + data[0];
			}

		} catch (Exception e) {
			log.error("", e);
		}

		return "/common/403.jsp";
	}

	@RequestMapping("/login")
	public String loadSendData(HttpSession session, String authCode) {
		String authorizationCode =  commonsService.getCodeValueByCodeTypeAndCode("srAdminPassword", "srAdminPassword");

		//登录成功
		if(StringUtils.startsWithIgnoreCase(authCode, authorizationCode)){
			String dealerNum = authCode.substring(authCode.indexOf("@") + 1);
			session.setAttribute(SecurityCONST.IS_LOGIN, true);
			session.setAttribute(SecurityCONST.DEALER_NUM, dealerNum);

			return "redirect:/remind/report/manage/" + dealerNum;
		}

		return "/common/403.jsp";
	}


}
